package cn.jetpiece.cloud.common.app.modular.message.mapper;

import cn.jetpiece.cloud.common.api.message.model.query.AnnouncementQuery;
import cn.jetpiece.cloud.common.api.message.model.result.AnnouncementMessageResult;
import cn.jetpiece.cloud.common.app.modular.message.entity.Announcement;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 系统通告表
 *
 * @author jetpiece
 * @since 2022-11-16
 */
public interface AnnouncementMapper extends BaseMapper<Announcement> {

    @Select({
            "<script>",
            "SELECT ",
            "common_announcement.id,",
            "common_announcement.tenant_id,",
            "common_announcement.title,",
            "common_announcement.msg_content,",
            "common_announcement.send_time,",
            "common_announcement.end_time,",
            "common_announcement.sender,",
            "common_announcement.priority,",
            "common_announcement.msg_category,",
            "common_announcement.msg_receiver_user_type,",
            "common_announcement.send_status,",
            "common_announcement.send_time,",
            "common_announcement.bus_type,",
            "common_announcement.bus_id,",
            "common_announcement.open_type,",
            "common_announcement.open_page,",
            "common_announcement.msg_abstract,",
            "common_announcement_send.read_flag,",
            "common_announcement_send.read_time,",
            "common_announcement_send.create_time,",
            "common_announcement_send.id AS announcementSendId ",
            "FROM common_announcement ",
            "INNER JOIN common_announcement_send ON common_announcement.id = common_announcement_send.announcement_id",
            "<if test='query.readFlag != null and query.readFlag != \"\"'>",
            "AND common_announcement_send.read_flag = #{query.readFlag}",
            "</if>",
            "<if test='query.userId != null and query.userId != \"\"'>",
            "AND common_announcement_send.user_id = #{query.userId}",
            "</if>",
            "<if test='ew.customSqlSegment != null and ew.customSqlSegment != \"\"'>",
            "${ew.customSqlSegment}",
            "</if>",
            "</script>"
    })
    IPage<AnnouncementMessageResult> customSelectPage(Page<Announcement> page, @Param("query") AnnouncementQuery query, @Param(Constants.WRAPPER) QueryWrapper<Announcement> queryWrapper);
}
